/*
		method:请求方式 post get
		url:请求地址
		params:接口传参
		token:是否要token
		success:成功的函数
		ContentType:请求头数据格式
	*/
function ajax({method,url,params,token,success,ContentType}) {
		let xhr = new XMLHttpRequest()

		/*
			{
				name:'zs',
				age:18
			}
			name=zs&age=18
		*/
		let str = ''
		for(let i in params){
			// i是属性名
			console.log(i)
			str += `${i}=${params[i]}&`
		}
		console.log(str)
		// xhr.open(method,url)
		if(method == 'get'){
			xhr.open(method,url + '?'+ str)
			if(token){
				xhr.setRequestHeader('token',token)
			}
			xhr.send()
		}else if(method =='getOther'){
			//
			let val = ''
			for(let j in params){
				val = params[j]
			}
			xhr.open('get',url + '/' +val)
			if(token){
				xhr.setRequestHeader('token',token)
			}
			xhr.send()
		}else if(method == 'post'){
			xhr.open(method,url)
			if(token){
				xhr.setRequestHeader('token',token)
			}
			//判断数据类型不同传参不同
			if(ContentType == 'application/x-www-form-urlencoded'){
				xhr.setRequestHeader('Content-Type',ContentType)
				xhr.send(str)
			}else if(ContentType =='application/json'){
				//json
				xhr.setRequestHeader('Content-Type',ContentType)
				xhr.send(JSON.stringify(params))
			}else{
				//文件上传
				// let fileParam = new FormData()
				// xhr.send(params.fileParam)
				let fileParam = new FormData()
				for(let i in params){
					fileParam.append(i,params[i])
				}
				xhr.send(fileParam)
			}

		}
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4){
				// 成功的结果返回出去
				success(JSON.parse(xhr.responseText))

			}
		}
	}
